package com.cidp.system.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.Tdepartment;
import com.cidp.system.pojo.DepartmentPo;
import com.cidp.system.service.IDepartmentService;
import com.litt.core.common.Constants;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;

/**
 * <b>标题：</b>系统部门.
 * <pre><b>描述</b>
 *    系统部门管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-6-3
 * @version 1.0
 */
public class DepartmentAction extends DispatchAction{
	
	private IDepartmentService departmentService;
	
	/**
	 * 查看系统部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DepartmentPo departmentPo = (DepartmentPo)dynaActionForm.get("departmentPo");
		
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = departmentService.listByPage(new PagePoParam(departmentPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("toList");
	}
	
	/**
	 * 保存系统部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DepartmentPo departmentPo = (DepartmentPo)dynaActionForm.get("departmentPo");
		
		Tdepartment department = new Tdepartment();
		department.setDepartCode(departmentPo.getDepartCode());
		department.setDepartName(departmentPo.getDepartName());
		//父部门？
		department.setParentDepartId(new Long(3));
		department.setRemark(departmentPo.getRemark());
		
		departmentService.saveDepartment(department);
		
		request.setAttribute("successFlag","department_saveDepartment");
		return mapping.findForward("toSave");
	}
	
	/**
	 * 新增系统部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 修改系统部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DepartmentPo departmentPo = (DepartmentPo)dynaActionForm.get("departmentPo");
		String id = request.getParameter("id");
		Tdepartment department = departmentService.loadDepartment(new Long(id));
		try{
			BeanUtils.copyProperties(departmentPo, department);
		}catch(Exception e){
			
		}
//		父部门？
		dynaActionForm.set("departmentPo", departmentPo);
		return mapping.findForward("toEdit");
	}
	
	/**
	 * 更新系统部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		DepartmentPo departmentPo = (DepartmentPo)dynaActionForm.get("departmentPo");
		
		Tdepartment department = departmentService.loadDepartment(departmentPo.getDepartId());
		department.setDepartCode(departmentPo.getDepartCode());
		department.setDepartName(departmentPo.getDepartName());
		//父部门？
	//	department.setParentDepartId(departmentPo.getParentDepartId());
		department.setRemark(departmentPo.getRemark());
		
		departmentService.updateDepartment(department);
		
		request.setAttribute("successFlag","department_updateDepartment");
		return mapping.findForward("toUpdate");
	}

	public void setDepartmentService(IDepartmentService departmentService) {
		this.departmentService = departmentService;
	}
}
